Coroutines

Lua Coroutines

The following paragraphs explain what Lua coroutines are and where they might be useful for programmers.

Similar to metatables, I know some Lua programmers that go about their day without knowing how coroutines work. And that's absolutely ok. They are still productive, can publish their games and their tasks, which proves that even if we don't understand completely coroutines we can still program in Lua and get stuff done.

This small article can be seen as an advanced topic, so If you do not want to learn about coroutines and multi-tasking in Lua, you  are free to skip this lecture.

Coroutines allow us to execute several tasks at once. Lua passes control to each routine in a controlled manner and waits until the routine says it has finished. We can reenter the routine to continue at a later time and by doing this repeatedly we achieve multi-tasking.

Each task runs in a thread which is separate from the other threads. Having several tasks running at once is often called multi-threading.

If you never heard the word thread before, it is basically a computer science term for a thread of execution. Threads are a way for a program to divide (split) itself into two or more simultaneously (or pseudo-simultaneously) running tasks.

There are different ways in which multi-threading can be implemented. Some systems allocate a fixed amount of time to each thread and take control away when the time is up, passing control on to the next thread. This is called pre-emptive multi-threading. In this case, each of the threads does not need to worry about how much time it occupies, it is more concerned with its own function.

In other systems, a thread is concerned with how long it is taking. The thread knows it must pass control to other threads so that they can function as well. This is called cooperative, or collaborative multi-threading. Here, all of the threads are collaborating together to allow the application to operate properly. This is the type of multi-tasking that Lua's coroutines use.

Coroutines in Lua are not operating system processes. Coroutines are blocks of Lua code which are created within Lua and have their own flow of control like threads. Only one coroutine ever runs at a time, and it runs until it activates another coroutine, or yields (returns to the coroutine that invoked it). Coroutines are a way to express multiple cooperating threads of control in a convenient and natural way, but do not execute in parallel, and thus gain no performance benefit from multiple CPU's. However, since coroutines switch much faster than operating system threads and do not typically require complex and sometimes expensive locking mechanisms, using coroutines is typically faster than the equivalent program using full OS threads.

In order for multiple coroutines to share execution they must stop executing (after performing a sensible amount of processing) and pass control to another thread. This act of submission is called yielding. Coroutines explicitly call a Lua function coroutine.yield(), which is similar to using return in functions. What differentiates yielding from function returns is that at a later point we can reenter the thread and carry on where we left off.

We can create a coroutine using the coroutine.create(f) function. We pass a Lua function as an entry point for the thread. The object returned by Lua is a thread.

A coroutine can be in one of three different states: suspended, running, and dead. When we create a coroutine, it starts in the suspended state. That means that a coroutine does not run its body automatically when we create it. We can check the state of a coroutine with the status function.

The state suspended means that the thread is alive, but it is not doing anything. To start the thread we use the coroutine.resume(c) function. Lua will enter the thread and leave when the thread yields.

Notice how we call resume(c) the first time and execute the function until it yields.

This is the interesting bit. With a normal function, we would not be able to carry on where we left off, but with coroutines, we can call resume again. We call coroutine.resume(c) one more time to see that we executed the line after the yield inside the function. However, if we now look at the status of the coroutine, we can see that we exited the function and the coroutine terminated with status dead.